/**
 * Created by lgq on 2015/9/1.
 */

var uploader;
jQuery(function() {
    var $ = jQuery,
        $list = $('#thelist'),
        $btn = $('#ctlBtn'),
        state = 'pending';

    uploader = WebUploader.create({

        //不压缩image
        resize: false,

        //swf文件路径
        swf: '/fsm/static/components/webuploader/Uploader.swf',

        //文件接收服务端。
        server: '/fsm/controller/fileSet/fileUpload',

        //选择文件的按钮
        pick: '#picker',

        //上传文件总数量
        fileNumLimit: 1,

        //单个上传文件大小1GB (最大支持上传文件总大小4GB)
        fileSingleSizeLimit: 1024*1024*1024

    });

    // 当文件被加入队列之前触发
    uploader.on('beforeFileQueued',function(file) {
        if(file.size > 1024*1024*1024){
            BootstrapDialog.alert({title: "信息", message: "文件大小超过1G！",buttonLabel : "确定"});
            return false;
        }
        var isPassed = checkFileFormat(file);
        if(!isPassed) {
            return false;
        }
        var isExistFile = isUploadedFile(file);
        if(isExistFile) {
            return false;
        }

    });

    // 当有文件添加进来的时候
    uploader.on('fileQueued', function( file ) {
        $list.append('<div id="' + file.id + '" class="item" size="' + file.size + '"> <h5 class="info"><span class="ti-trash" style="cursor: pointer;" onclick=removeFile("' + file.id + '");></span> ' + file.name + '</h5> <p class="state">等待上传...</p> </div>');
    });

    // 文件上传过程中创建进度条实时显示。
    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');
        // 避免重复创建
        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active"> <div class="progress-bar" role="progressbar" style="width: 0%"> </div> </div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('p.state').text('上传中');

        $percent.css( 'width', percentage * 100 + '%' );
    });

    uploader.on( 'uploadSuccess', function( file ,response) {
        var message = response.message;
        var fileId = response.result;
        var fileName = response.fileName;
        var scormJsonData = response.scormJsonData;
        $( '#'+file.id ).find('p.state').text(message).addClass("text-success");
        if(fileId!=""){
            uploadResult(message,fileId);
            setFileIdInfo(message,fileId,fileName,scormJsonData);
        }
    });

    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错').addClass("text-danger");
    });

    uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });

    uploader.on( 'all', function( type ) {
        if ( type === 'startUpload' ) {
            state = 'uploading';
        } else if ( type === 'stopUpload' ) {
            state = 'paused';
        } else if ( type === 'uploadFinished' ) {
            state = 'done';
        }

        if ( state === 'uploading' ) {
            $btn.text('暂停');
        } else {
            $btn.text('上传');
        }
    });

    $btn.on( 'click', function() {
        if ( state === 'uploading' ) {
            uploader.stop();
        } else {
            uploader.upload();
        }
    });


});

function removeFile(fileId){
    $("#"+fileId).remove();
    $("#relationInfo").val("");
    $("#relationFileName").val("");
    $("#fileFormat").val("");
    uploader.removeFile(uploader.getFile(fileId));
}

function uploadResult(message,result){
    var resultURL = $("#resultURL").val();
    resultURL+="?message="+message;
    resultURL+="&result="+result;
    var resultHtml = '<iframe frameborder="0" style="display:none" src="'+resultURL+'"></iframe>';
    $("#uploadResult").html(resultHtml);
}

function checkFileFormat(file) {}

function isUploadedFile(file) {}

function setFileIdInfo(message,fileId,fileName){}

